var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()

Page({
  data: {
    report_id: 0,
    info: null,
    verify_h: '', // 核实情况
    verify_z: '', // 作废理由
    isLose: false, // 打开忽略弹框
    isFocus: false, //是否获取文本框的焦点
    isShowTextarea: false, //是否显示文本框
    mainEquipImage: '', // 主设备图片
    partEquipImage: '', // 局部设备图片
    nameplateImage: '', // 设备铭牌图片
    invalidReason:'',//作废原因
  },
  onLoad: function (options) {
    this.setData({
      report_id: options.report_id || 0,
      ticket_num:options.ticket_num
    })
    this.getInfo()
  },
  onShow: function () {

  },
  getInfo() { // 详情数据
    var params = {
      report_id: this.data.report_id,
      ticket_num:this.data.ticket_num
    }
    var that=this;
    api.$get(api.repairsManage.info, params).then((res) => {
      that.setData({
        info: res.data,
        verify_h:res.data.verify,
        mainEquipImage: res.data.mainEquipImage, // 主设备图片
        partEquipImage: res.data.partEquipImage, // 局部设备图片
        nameplateImage: res.data.nameplateImage, // 设备铭牌图片
      })
      console.log( that.data.mainEquipImage)
    })
  },
  //##input 输入##
  input_handle(e) {
    this.setData({
      [e.currentTarget.dataset.key]: e.detail.value
    })
  },
  //##input 输入##

  toFocusHandle() { //解决textarea placeholder 层级过高的bug 微信好坑 
    this.setData({
      isShowTextarea: true
    })
    setTimeout(() => {
      this.setData({
        isFocus: true
      })
    }, 100)
  },
  upPicsHandle(e) { // ##图片组件传出来的数据##
    var { pics, key } = e.detail
    console.log(pics, key)
    this.setData({
      [key]: pics.length>0?pics[0]:[]
    })
   
  },
  textarea_blur_handle() { //解决textarea placeholder 层级过高的bug 微信好坑
    this.setData({
      isFocus: false,
      isShowTextarea: false
    })
  },

  openLoseHandle() { // 打开忽略弹框
    this.setData({ isLose: true })
  },

  loseConfirmHandle(){ // 作废
    if($.isNull(this.data.invalidReason)){
      $.toast("作废原因不能为空")
        return
    }
    var params = {
      "ticket_num": this.data.ticket_num,
      "deal_style":'已作废',//状态变为 3：已作废
      "invalidReason":this.data.invalidReason,//作废原因
      "invalidPersonid":wx.getStorageSync('user_id'),//作废人id
      "invalidPerson":wx.getStorageSync('user_name'),//作废人
    }
    api.$post(api.repairsManage.setZuoFei, params).then((res) => {
      $.toast('作废成功')
      setTimeout(() => {
        $.linkurl('', 'back')
      }, 1500)
    })
  },

  updateHandle() { // 更新报修内容
    if(!$.isNull(this.data.info.verify)){
      $.toast("已核实情况,请勿重复操作")
      return
    }
    if($.isNull(this.data.verify_h)){
      $.toast("请填写核实情况")
      return
    }
    var params = {
      ticket_num: this.data.ticket_num,
      verify: this.data.verify_h
    }
    api.$post(api.repairsManage.update, params).then((res) => {
      $.toast('更新成功')
      setTimeout(() => {
        $.linkurl('', 'back')
      }, 1500)
    })
  },

  toDefectManageFlawRegister(e) { // 缺陷登记
    var { item } = $.detail(e)
    $.linkurl(`/pages/defectManage/defectManageFlawRegister/defectManageFlawRegister?info=${JSON.stringify(this.data.info)}&type=1`)
  },
  toRobManageRushSend(e) { // 去抢修
    if($.isNull(this.data.info.verify)){
      $.toast("请填写核实情况,点击核实按钮")
      return
    }
    $.linkurl(`/pages/robManage/robManageRushSend/robManageRushSend?info=${JSON.stringify(this.data.info)}&u_i=2`)
  },
  callHandle(e) {
    var { phone } = $.detail(e)
    $.call(phone)
  },
  toRobManage(){//去检修
    if($.isNull(this.data.info.verify)){
      $.toast("请填写核实情况,点击核实按钮")
      return
    }
          var data=this.data.info;
          var info1={
            company:data.company, //'企业用户',
            company_id:data.dept_id, 
            factory_id:data.STATIONID, //'厂站名称',
            factory:data.factory, //'厂站名称',
            equip:data.equipment, // '设备名称', 
            source:'报修',
            number:data.ticket_num,
          }
          $.linkurl(`/pages/checkManage/checkManageAdd/checkManageAdd?type=0&info=${JSON.stringify(info1)}`)
  }
  
})